package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.InsCompanyMapper;
import com.gxa.pingan.pojo.InsCompany;
import com.gxa.pingan.service.InsCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lijianmin
 * @date 2021/10/26  15:22:43
 */
@Service("insCompanyService")
@Transactional(rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public class InsCompanyServiceImpl implements InsCompanyService {

    @Autowired
    private InsCompanyMapper insCompanyMapper;

    /**
     * 保险公司列表查询的方法
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @Override
    public List<InsCompany> getList(Integer page, Integer size) throws BusinessException {
        if (page != null) {
            if (page <= 0) {
                throw new BusinessException(ExceptionCode.PAGE_ERROR);
            } else {
                page = (page - 1) * size;
            }
        } else {
            page = 0;
        }
        if (size != null) {
            if (size <= 0) {
                throw new BusinessException(ExceptionCode.SIZE_ERROR);
            }
        } else {
            size = 10;
        }

        return insCompanyMapper.getList(page, size);
    }

    /**
     * 保险公司列表计数的方法
     *
     * @return 返回查询到的总数
     */
    @Override
    public Integer getListCount() throws BusinessException {
        return insCompanyMapper.getList(null, null).size();
    }

    /**
     * 添加保险公司的方法
     *
     * @param insCompany 添加的保险公司对象
     * @return 返回是否成功
     */
    @Override
    public Boolean add(InsCompany insCompany) throws BusinessException {
        if (!insCompany.getPhone().isEmpty()) {
            InsCompany insCompany1 = insCompanyMapper.selectByPhone(insCompany.getPhone());
            if (insCompany1 != null) {
                throw new BusinessException(ExceptionCode.REPEAT_PHONE);
            }
        }
        if (!insCompany.getName().isEmpty()) {
            InsCompany insCompany2 = insCompanyMapper.selectByName(insCompany.getName());
            if (insCompany2 != null) {
                throw new BusinessException(ExceptionCode.REPEAT_PHONE);
            }
        }
        Integer add = insCompanyMapper.add(insCompany);
        return add != 0;
    }

    /**
     * 修改保险公司的方法
     *
     * @param insCompany 修改的保险公司对象
     * @return 返回是否成功
     */
    @Override
    public Boolean update(InsCompany insCompany) throws BusinessException {
        InsCompany insCompanyId = insCompanyMapper.selectById(insCompany.getId());
        if (insCompany.getPhone() != null) {
            InsCompany insCompanyPhone = insCompanyMapper.selectByPhone(insCompany.getPhone());
            if (insCompanyPhone != null && !insCompanyPhone.getId().equals(insCompanyId.getId())) {
                throw new BusinessException(ExceptionCode.REPEAT_PHONE);
            }
        }
        if (insCompany.getName() != null) {
            InsCompany insCompanyName = insCompanyMapper.selectByName(insCompany.getName());
            if (insCompanyName != null && !insCompanyName.getId().equals(insCompanyId.getId())) {
                throw new BusinessException(ExceptionCode.REPEAT_COMPANY_NAME);
            }
        }
        Integer update = insCompanyMapper.update(insCompany);
        return update != 0;
    }

    /**
     * 删除保险公司的方法
     *
     * @param id 删除的保险公司的id
     * @return 返回是否成功
     */
    @Override
    public Boolean delete(Long id) throws BusinessException {
        InsCompany insCompany = insCompanyMapper.selectById(id);
        if (insCompany == null) {
            throw new BusinessException(ExceptionCode.NO_COMPANY);
        }
        Integer delete = insCompanyMapper.delete(id);
        return delete != 0;
    }

    @Override
    public List<InsCompany> findAll() {
        return insCompanyMapper.findAll();
    }
}
